import { TMakePlugin, PluginAPI, PluginMetadata, PluginLifecycleEvent } from '../types';


const metadata: PluginMetadata = {
  name: 'legacy-extensions',
  version: '1.0.0',
  description: '迁移自原始extensions.ts的内置扩展功能',
  author: 'TMake Team',
  isCore: true
};


const legacyExtensionsPlugin: TMakePlugin = {
  metadata,
  
  
  init: (api: PluginAPI) => {
    /**
     * 设置目标平台
     * @description 设置编译目标平台
     * @param {string} platform - 目标平台名称，可选值：windows, linux, macos, android, ios
     * @returns {string} 返回设置的平台名称
     */
    api.registerFunction('设置目标平台', (node, context) => {
      if (!node.params || node.params.length === 0) {
        throw new Error('设置目标平台函数需要一个参数');
      }
      
      const platform = node.params[0]?.value || node.params[0];
      const validPlatforms = ['windows', 'linux', 'macos', 'android', 'ios'];
      
      if (!validPlatforms.includes(platform.toLowerCase())) {
        throw new Error(`不支持的平台: ${platform}，支持的平台有: ${validPlatforms.join(', ')}`);
      }
      
      api.config.platform = platform.toLowerCase();
      return platform;
    });
    
    
    /**
     * 路径拼接
     * @description 拼接多个路径片段
     * @param {...string} paths - 要拼接的路径片段
     * @returns {string} 返回拼接后的路径
     */
    api.registerFunction('路径拼接', (node, context) => {
      if (!node.params || node.params.length === 0) {
        throw new Error('路径拼接函数需要至少一个参数');
      }
      
      const paths = node.params.map((param: any) => param?.value || param).filter(Boolean);
      const result = paths.join('/');
      return result;
    });
    
    
    /**
     * 字符串拼接
     * @description 拼接多个字符串
     * @param {...string} strings - 要拼接的字符串
     * @returns {string} 返回拼接后的字符串
     */
    api.registerFunction('字符串拼接', (node, context) => {
      if (!node.params || node.params.length === 0) {
        throw new Error('字符串拼接函数需要至少一个参数');
      }
      
      const strings = node.params.map((param: any) => param?.value || param).filter(Boolean);
      const result = strings.join('');
      return result;
    });
    
    
    /**
     * 设置优化级别
     * @description 设置编译器优化级别
     * @param {string|number} level - 优化级别，可选值：0, 1, 2, 3, s, fast
     * @returns {string|number} 返回设置的优化级别
     */
    api.registerFunction('设置优化级别', (node, context) => {
      if (!node.params || node.params.length === 0) {
        throw new Error('设置优化级别函数需要一个参数');
      }
      
      const level = node.params[0]?.value || node.params[0];
      const validLevels = ['0', '1', '2', '3', 's', 'fast'];
      
      if (!validLevels.includes(level.toString())) {
        throw new Error(`不支持的优化级别: ${level}，支持的级别有: ${validLevels.join(', ')}`);
      }
      
      api.config.optimizeLevel = parseInt(level.toString(), 10);
      return level;
    });
    
    // 移除了完成日志
  },
};


export default legacyExtensionsPlugin;
export { legacyExtensionsPlugin };